import { useCallback, useState } from 'react'
import {
  getBannerList,
  getArchiveList,
  getCVList,
  getCVTree,
  getCalendar,
  getXinFanList,
} from '@/api'

// interface Banner {
//   id: number
//   name: string
//   poster: string
//   pv: string
//   type: string
// }

interface BannerListResponse {
  // bannerVOList: Banner[]
  // md5Hash: string
  // name: string
  // time: number
    [key: string]: any
}

// -----------------------------------

interface ArchiveResponse {
  // id: string
  // name: string
    [key: string]: any
}

interface CvListResponse {
  // code: string
  // cover: string
  // id: number
  // name: string
  // url: string
    [key: string]: any
}

// -----------------------------------

// type Category = {
//   color: string
//   label: string
// }
//
// type Edge = {
//   categorie: string
//   categorie2: string
//   direction: boolean
//   from: number
//   id: number
//   label: string
//   to: number
// }
//
// type Node = {
//   categorie: string
//   id: number
//   image: string
//   info: string
//   label: string
// }

// -----------------------------------

type CVListData = {
  // categories: Record<string, Category>
  // data: {
  //   edges: Edge[]
  //   nodes: Node[]
  // }
    [key: string]: any
}

// -----------------------------------

interface CalendarItem {
  // weekday: WeekDay
  // data: any
    [key: string]: any
}

// type WeekDay = {
//   id: string | number
//   cn: string
//   en: string
// }

// type AnimeItemInfo = {
//   id: string | number
//   fanName: string
//   poster: string
//   total: number
//   tag: string
//   descInfo: string
// }

interface AllItem {
    // weekday: WeekDay
    // data: any
    [key: string]: any
}

export const useGetBannerList = () => {
  const [banner, setBannner] = useState<BannerListResponse | null>(null)
  const [loading, setLoading] = useState(false)

  const updateBannerList = useCallback(async (selectedValue: string) => {
    try {
      setLoading(true)
      let response = await getBannerList(selectedValue)
      setBannner(response)
      setLoading(false)
      return response
    } catch (error) {}
  }, [])

  return {
    banner,
    setBannner,
    updateBannerList,
    loading,
  }
}

export const useGetArchiveList = () => {
  const [archive, setArchive] = useState<ArchiveResponse>([])
  const [loading, setLoading] = useState(false)

  const updateArchiveList = useCallback(async () => {
    try {
      setLoading(true)
      let response = await getArchiveList()
      setArchive(response)
      setLoading(false)
      return response
    } catch (error) {}
  }, [])

  return {
    archive,
    setArchive,
    updateArchiveList,
    loading,
  }
}

export const useGetCVList = () => {
  const [cvlist, setCVList] = useState<CvListResponse>([])
  const [loading, setLoading] = useState(false)

  const updateCVList = useCallback(async () => {
    try {
      setLoading(true)
      let response = await getCVList()
      setCVList(response)
      setLoading(false)
      return response
    } catch (error) {}
  }, [])

  return {
    cvlist,
    setCVList,
    updateCVList,
    loading,
  }
}

export const useGetCVTree = () => {
  const [cvtree, setCVTree] = useState<CVListData | null>(null)
  const [loading, setLoading] = useState(false)

  const updateCVTree = useCallback(async (selectedValue: string) => {
    try {
      setLoading(true)
      let response = await getCVTree(selectedValue)
      setCVTree(response)
      setLoading(false)
      return response
    } catch (error) {}
  }, [])

  return {
    cvtree,
    setCVTree,
    updateCVTree,
    loading,
  }
}

export const useGetAnime = () => {
  const [anime, setAnime] = useState<CalendarItem>([])
  const [loading, setLoading] = useState(false)

  const updateAnime = useCallback(async () => {
    try {
      setLoading(true)
      let response = await getCalendar()
      setAnime(response)
      setLoading(false)
      return response
    } catch (error) {}
  }, [])

  return {
    anime,
    setAnime,
    updateAnime,
    loading,
  }
}


export const useGetXinFanList = () => {
    const [xinfan, setXinFan] = useState<AllItem>([])
    const [loading, setLoading] = useState(false)
    const [internalPlayer, setInternalPlayer] = useState(true)

    const updateXinFanList = useCallback(async (id:any, ep:any) => {
        try {
            setLoading(true)
            let response = await getXinFanList(id, ep)
            setXinFan(response)
            setLoading(false)
            return response
        } catch (error) {}
    }, [])

    return {
        xinfan,
        internalPlayer,
        setInternalPlayer,
        setXinFan,
        updateXinFanList,
        loading,
    }
}

